package thread;

/**
 * 线程的定义方法
 */

class thread1 extends Thread{
    //run内部是线程需要执行的内容,相当于安排工作,但线程并没创建出来
    @Override
    public void run(){
        System.out.println("thread1");
    }
}

class thread2 implements Runnable{
    @Override
    public void run() {
        System.out.println("thread2");
    }
}

public class TestDemo {
    public static void main(String[] args) {
        //第一种方式:继承Thread
        Thread thread1 = new thread1();
        //start是正式开始干活
        thread1.start();

        //第二种方式:实现Runnable接口
        Thread thread2 = new Thread(new thread2());
        thread2.start();

        //第三种方式:匿名内部类
        Thread thread3 = new Thread(){
            @Override
            public void run() {
                System.out.println("thread3");
            }
        };
        thread3.start();

        //第四种方式:Runnable的匿名内部类
        Thread thread4 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread4");
            }
        });
        thread4.start();

        //第五种方式:lambda表达式,这里是代替了Runnable接口
        Thread thread5 = new Thread(() ->{
            System.out.println("thread5");
        });
        thread5.start();

        //用Runnable方式写线程更好,它只描述了任务,不关心由谁来执行,并且接口不关心多继承问题
    }
}
